using System.CodeDom;
using System.Collections.Generic;
using InterfaceWeaver.AopCore;
using Roslyn.Compilers.CSharp;

namespace InterfaceWeaver.CustomTool
{
    public class AddPropertyNameConstantAspectHandler : AspectHandlerBase<AddPropertyNameConstantAttribute>
    {
        /// <summary>
        /// Handles the on property.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="attributeSyntax">The attribute syntax.</param>
        /// <param name="propertyDeclarationSyntax"> </param>
        /// <param name="handlerStreams"> </param>
        /// <returns>
        /// True if the aspect is handled, false otherwise
        /// </returns>
        public override void HandleOnProperty(AspectDispatcher context, AttributeSyntax attributeSyntax, PropertyDeclarationSyntax propertyDeclarationSyntax, IDictionary<PropertyDeclarationSyntax, AspectTuple[]> handlerStreams)
        {
            var currentType = context.WeavedCodeContext.CurrentTypeDeclaration;
            var currentProperty = context.WeavedCodeContext.CurrentCodeMemberPropertyDeclaration.CurrentCodeMemberPropertyDeclaration;

            var constant = new CodeMemberField(
                typeof(string), 
                string.Format("{0}PropertyName", currentProperty.Name))
                {
                    Attributes = MemberAttributes.Const | MemberAttributes.Private,
                    InitExpression = new CodePrimitiveExpression(currentProperty.Name)
                };
            
            currentType.Members.Add(constant);
        }
    }
}